package com.example.tourism.service.impl;

import com.example.tourism.mapper.HomeMapper;
import com.example.tourism.pojo.Result;
import com.example.tourism.pojo.mo.CommentMo;
import com.example.tourism.pojo.po.HotelInfo;
import com.example.tourism.pojo.po.Img;
import com.example.tourism.pojo.po.Room;
import com.example.tourism.pojo.vo.res.HotelDetailVo;
import com.example.tourism.pojo.vo.res.RecommendHotelResVo;
import com.example.tourism.pojo.vo.res.RoomDetailResVo;
import com.example.tourism.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    private HomeMapper  homeMapper;
    @Override
    public Result getRecommendHotels() {
        // 直接查询数据
        List<HotelInfo> recommendHotels = homeMapper.getRecommendHotels();
        List<RecommendHotelResVo> recommendHotelResVos = new ArrayList<>();
        recommendHotels.forEach(recommendHotel -> {
            // 封装数据
            RecommendHotelResVo recommendHotelResVo = new RecommendHotelResVo();
            Integer hotelId = recommendHotel.getId();
            //查寻照片
            Img img  = homeMapper.getHotelImg(hotelId);
            recommendHotelResVo.setImage(img.getUrl());

            recommendHotelResVo.setId(recommendHotel.getId());
            recommendHotelResVo.setName(recommendHotel.getHotelName());
            // 查询评价
            CommentMo commentMo = homeMapper.selectHotelComment(hotelId);
            if(commentMo != null) {
                recommendHotelResVo.setReviews(String.valueOf(commentMo.getCommentNumber()));
                //检测水平
                if (commentMo.getAvgRating() >= 2.5) {
                    if(commentMo.getAvgRating() >= 4.5){
                       recommendHotelResVo.setComment("超棒");
                    }else{
                       recommendHotelResVo.setComment("不错");
                    }
                }
            }
            // 查询userId
          Integer userId = homeMapper.getUserIdBYHotelId(hotelId);
            // 查询价格
           String price =  homeMapper.selectHotelMinPrice(userId);
           recommendHotelResVo.setPrice(price);
           recommendHotelResVos.add(recommendHotelResVo);
        });
        return  Result.success(recommendHotelResVos);
    }

    @Override
    public Result getHotelDetail(String id) {
        //查询得到完整的酒店信息
        // 查询优惠卷（
//        List<String> coupons = homeMapper.getHotelCoupons(id);

        HotelInfo hotelInfo = homeMapper.getHotelDetail(id);
        HotelDetailVo hotelDetailVo = new HotelDetailVo();
        hotelDetailVo.setHotelId(hotelInfo.getId());
        hotelDetailVo.setHotelName(hotelInfo.getHotelName());
        hotelDetailVo.setAddress(hotelInfo.getHotelAddress());
        hotelDetailVo.setDescription(hotelInfo.getDescription());
        // 查询照片
        List<Img> imgs  = homeMapper.getHotelImgs(hotelInfo.getId());
        List<String>  imgUrls = new ArrayList<>(imgs.size());
        imgs.forEach(img -> {
            imgUrls.add(img.getUrl());
        });
        hotelDetailVo.setPhotos(imgUrls);

        // 查询评分
        Double rating = homeMapper.getHotelRating(hotelInfo.getId());
        // 如果有小数，保留两位小数，如果没有则显示整数
        if(rating != null){
            if(rating % 1 == 0){
                hotelDetailVo.setRating(String.valueOf(rating.intValue()));
            }else{
                // 保留两位小数
                rating = (double) Math.round(rating * 100) / 100;
                hotelDetailVo.setRating(String.valueOf(rating));
            }
        }

        // 查询房间类型
        List<String> roomTypes = homeMapper.getRoomTypes(id);
        hotelDetailVo.setRoomTypes(roomTypes);

        return Result.success(hotelDetailVo);
    }

    @Override
    public Result getRoomList( String hotelId) {
        // 得到房间列表
        //int start = (Integer.parseInt(currentPage)-1)*  Integer.parseInt(pageSize);
        String userId = String.valueOf(homeMapper.getUserIdBYHotelId(Integer.valueOf(hotelId)));
        List<Room> roomList = homeMapper.getRoomList(userId);
        // 封装数据
        List<RoomDetailResVo> roomDetailResVos = new ArrayList<>(roomList.size());
        for (Room room : roomList) {
            RoomDetailResVo roomDetailResVo = new RoomDetailResVo();
            roomDetailResVo.setId(String.valueOf(room.getId()));
            roomDetailResVo.setName(room.getName());
            roomDetailResVo.setOriginalPrice(room.getPrice());
            // 检测type，进行赋值
            // 处理死数据
            if(room.getType().equals("单人间")){
                roomDetailResVo.setBeds("1张1.8米大床");
                roomDetailResVo.setSize("25-35㎡");
                roomDetailResVo.setBreakfast("一份早餐");
            }else if(room.getType().equals("双人间")){
                roomDetailResVo.setBeds("2张1.2米单人床");
                roomDetailResVo.setSize("25-28㎡");
                roomDetailResVo.setBreakfast("两份早餐");
            }else if(room.getType().equals("套房")){
                roomDetailResVo.setBeds("1张1.8米大床");
                roomDetailResVo.setSize("30-40㎡");
                roomDetailResVo.setBreakfast("一份早餐");
            }

            List<String> features =new ArrayList<>();
            features.add("免费Wi-Fi");
            features.add("免费停车");
            roomDetailResVo.setFeatures(features);
            roomDetailResVo.setAvailability("1");

            // 查询照片
            Img img = homeMapper.getRoomImg(room.getId());
            if(img == null){
                roomDetailResVo.setImage("https://tse3-mm.cn.bing.net/th/id/OIP-C.b0JXPDgh7fsWp_uEOAM65AAAAA?w=153&h=180&c=7&r=0&o=5&pid=1.7");
            }else {
                roomDetailResVo.setImage(img.getUrl());
            }
            // 要处理优惠劵
            roomDetailResVos.add(roomDetailResVo);
        }
        return Result.success(roomDetailResVos);
    }
}
